package pixelwaks.gui.impl;

import java.awt.*;

import javax.swing.*;

import common.gui.IViewable;

import pixelwaks.core.impl.Game;
import pixelwaks.core.impl.Player;
import pixelwaks.ctrl.ControlFactory;

public class PlayerScore extends JComponent implements IViewable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String playerName;
	private Player player;
	private Boolean currentPlayer;
	private Font arial12;
	private Font arialBold12;
	private int scoreBarWidth;
	private int scoreBarHeight;
	private int xPos;
	private int yPos;
	private int width;
	private int height;

	public PlayerScore(Player player) {

		this.player = player;
		scoreBarWidth = 206;
		scoreBarHeight = 23;
		xPos = 10;
		yPos = 10;
		width = scoreBarWidth + xPos*2;
		height = scoreBarHeight + yPos*2;

		setPreferredSize(new Dimension(width, height));
		setVisible(true);

		ControlFactory.getObserver(ControlFactory.PLAYER_CTRL).addView(this);

		//fonts
		arial12 = new Font("Arial", Font.PLAIN, 12);
		arialBold12 = new Font("Arial", Font.BOLD, 12);
		playerName = player.getName();
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if(Game.getInstance().getCurrentPlayer() == player)
			currentPlayer = true;
		else
			currentPlayer = false;

		double playerStatus = player.getStatus();

		g2.setColor(Color.BLACK);
		if(currentPlayer)
			g2.fillRect(xPos, yPos, scoreBarWidth, scoreBarHeight);
		else
			g2.fillRect(xPos+1, yPos+1, scoreBarWidth-2, scoreBarHeight-2);
		
		g2.setColor(Color.WHITE);
		g2.fillRect(xPos+3, yPos+3, scoreBarWidth-6, scoreBarHeight-6);
		g2.setColor(Color.BLACK);
		g2.setStroke(new BasicStroke(2));
		g2.drawLine(xPos + scoreBarWidth - 80, yPos+2, xPos + scoreBarWidth - 80, yPos + scoreBarHeight-2);
		
		if(currentPlayer)
			g2.setFont(arialBold12);
		else
			g2.setFont(arial12);
		g2.drawString(playerName, xPos+7, height/2 + g2.getFontMetrics().getHeight()/3);

		//g2.setFont(arial12);
		String score;

		score = (int)playerStatus + "p";
		g2.drawString("Score: ", xPos + scoreBarWidth - 75, height/2 + g2.getFontMetrics().getHeight()/3);
		g2.drawString(score, xPos+scoreBarWidth - g2.getFontMetrics().stringWidth(score) - 6, height/2 + g2.getFontMetrics().getHeight()/3);
	}

	@Override
	public void updateView(Object o) {
		repaint();
	}
}
